//
//  LGTimeTools.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGTimeTools.h"
#import "NSString+Tool.h"

@implementation LGTimeTools
+ (NSString *)compareYear:(NSString *)toDateString {
    if (![toDateString isNotBlank]) {
        return @"";
    }
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /* sheny 17-4-21 更改 NSCalendarUnitYear */
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    int year = (int)[dateComponent year];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *toDate = [dateformatter dateFromString:toDateString];
    
    NSDateComponents *toDateComponent = [calendar components:unitFlags fromDate:toDate];
    int toYear = (int)[toDateComponent year];
    
    int tempYer = year - toYear;
    
    return [NSString stringWithFormat:@"%d年",tempYer < 0 ? 0 : tempYer];
}


+ (NSInteger)getAgeFromBirthDay:(NSString*)birth {
    //1992-07-02
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *myDate= [dateFormatter dateFromString:birth];
    if(myDate == nil){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        myDate = [dateFormatter dateFromString:birth];
    }
    if (!myDate)
    {
        return 0;
    }
    /* sheny 17-4-21 更改 NSCalendarUnitDay */
    // 出生日期转换 年月日
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:myDate];
    NSInteger brithDateYear  = [components1 year];
    NSInteger brithDateMonth = [components1 month];
    NSInteger brithDateDay   = [components1 day];
    
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateMonth = [components2 month];
    NSInteger currentDateDay   = [components2 day];
    
    // 计算年龄
    //申洋 3.2 第二轮bug修复 将currentDateDay <= brithDateDay  改成 currentDateDay < brithDateDay
    NSInteger iAge = currentDateYear - brithDateYear;
    if ((currentDateMonth < brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay < brithDateDay)) {
        //周岁是公历生日的第二天起算
        iAge--;
    }
    return iAge;
}


+ (NSDate*)dateFromShortString:(NSString*)string {
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateformatter dateFromString:string];
    return date;
    
}


+ (NSDate*)dateFromString:(NSString*)string {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:string];
    return date;
}


+ (NSString *)formatDateWithDate:(NSDate*)date {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateformatter stringFromDate:date];
}


+ (NSString *)formatYearMonthDayStringWithDate:(NSDate *)date {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    return [dateformatter stringFromDate:date];
}


+ (NSString *)getweekStringFromDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//设置成中国阳历
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;//设置时区
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger weekNumber = [comps weekday]; //获取星期对应的长整形字符串
    
    NSString *weekDay;
    switch (weekNumber) {
        case 1:
            weekDay=@"周日";
            break;
        case 2:
            weekDay=@"周一";
            break;
        case 3:
            weekDay=@"周二";
            break;
        case 4:
            weekDay=@"周三";
            break;
        case 5:
            weekDay=@"周四";
            break;
        case 6:
            weekDay=@"周五";
            break;
        case 7:
            weekDay=@"周六";
            break;
            
        default:
            break;
    }
    return weekDay;
}


+ (NSString *)formatDate:(NSString *)dateStr mode:(NSInteger)mode {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:dateStr];
    
    if (date)
    {
        switch (mode) {
            case 1:
                [dateformatter setDateFormat:@"yyyy-MM-dd"];
                break;
            case 2:
                [dateformatter setDateFormat:@"yyyy年MM月dd日"];
                break;
            case 3:
                [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
                break;
            default:
                break;
        }
        return [dateformatter stringFromDate:date];
    }
    else
    {
        return dateStr;
    }
}


+ (NSString *)mouthDayStringFromString:(NSString *)string {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:string];
    
    /* sheny 17-4-21 更改  NSCalendarIdentifierGregorian */
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//设置成中国阳历
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;//设置时区
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger day=[comps day];//获取日期对应的长整形字符串
    NSInteger month=[comps month];//获取月对应的长整形字符串
    
    return [NSString stringWithFormat:@"%lu-%lu",month,day];
}


+ (NSString *)newMouthDayString:(NSString *)string {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:string];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//设置成中国阳历
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;//设置时区
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger day=[comps day];//获取日期对应的长整形字符串
    NSInteger month=[comps month];//获取月对应的长整形字符串
    
    return [NSString stringWithFormat:@"%lu月%lu日",month,day];
}


+ (NSString *)getweekStringFromDateString:(NSString *)dateString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:dateString];
    
    if (!date) {
        return nil;
    }
    
    return [self getweekStringFromDate:date];
}


+ (NSString *)getHourMinuteFromDateString:(NSString *)dateString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:dateString];
    [dateformatter setDateFormat:@"HH:mm"];
    return [dateformatter stringFromDate:date];
}


+ (NSString *)getMonthDayHourMinuteStringFromString:(NSString *)dateString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    if (dateString.length == 19) {
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    } else {
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    
    NSDate *date = [dateformatter dateFromString:dateString];
    [dateformatter setDateFormat:@"MM-dd HH:mm"];
    NSString *newDateStr = [dateformatter stringFromDate:date];
    return newDateStr == nil ? dateString : newDateStr;
}


+ (NSString *)prettyDateWithReference:(NSString *)dateString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = [dateformatter dateFromString:dateString];
    if (!date)
    {
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        date = [dateformatter dateFromString:dateString];
    }
    
    if (date)
    {
        NSTimeInterval secondsPerDay = 24 * 60 * 60;
        
        //修正8小时之差
        NSDate *date1 = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date1];
        NSDate *localeDate = [date1  dateByAddingTimeInterval: interval];
        
        NSDate *today = localeDate;
        NSDate *yesterday;
        //今年
        yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
        
        // 10 first characters of description is the calendar date:
        NSString *todayString = [[today description] substringToIndex:10];
        NSString *yesterdayString = [[yesterday description] substringToIndex:10];
        
        NSString *dateDayString = [[date description] substringToIndex:10];
        
        NSString *dateContent;
        //今 昨 前天的时间
        NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
        //其他时间
        NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];
        if ([dateDayString isEqualToString:todayString])
        {
            NSTimeInterval time = [today timeIntervalSinceDate:date];
            //******* 刚申请的时间应为1分钟内 *******
            if (time < 60) {
                dateContent = @"1分钟内";
            }
            else if(time >= 60 && time < 3600)
            {
                dateContent = [NSString stringWithFormat:@"%.f分钟前",time/60];
            }
            else if (time >= 3600)
            {
                dateContent = [NSString stringWithFormat:@"%.f小时前",time/3600];
            }
            return dateContent;
        }
        else if ([dateDayString isEqualToString:yesterdayString]){
            dateContent = [NSString stringWithFormat:@"昨天 %@",time];
            return dateContent;
        }
        else{
            return time2;
        }
        
    }
    else {
        return dateString;
    }
}

+ (NSString *)generateDifferentTypes:(NSString *)dateString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = [dateformatter dateFromString:dateString];
    
    if (!date)
    {
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        date = [dateformatter dateFromString:dateString];
    }
    
    if (date)
    {
        NSTimeInterval secondsPerDay = 24 * 60 * 60;
        
        //修正8小时之差
        NSDate *date1 = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date1];
        NSDate *localeDate = [date1  dateByAddingTimeInterval: interval];
        
        NSDate *today = localeDate;
        NSDate *yesterday;
        //今年
        yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
        
        // 10 first characters of description is the calendar date:
        NSString *todayString = [[today description] substringToIndex:10];
        NSString *yesterdayString = [[yesterday description] substringToIndex:10];
        
        NSString *dateDayString = [[date description] substringToIndex:10];
        
        //今 昨 前天的时间
        NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
        //其他时间
        NSString *time2 = [[date description] substringWithRange:(NSRange){5,5}];
        
        //今天
        if ([dateDayString isEqualToString:todayString])
        {
            return time;
        }
        //昨天
        else if ([dateDayString isEqualToString:yesterdayString])
        {
            return @"昨日";
        }
        //昨天以前
        else
        {
            return time2;
        }
        
    }
    else {
        return dateString;
    }
}


+(BOOL)compareNow:(NSString *)compareTime {
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *compareDate = [dateformatter dateFromString:compareTime];
    
    NSDate *nowDate = [NSDate date];
    
    NSTimeInterval _comT = [compareDate timeIntervalSince1970]*1;
    NSTimeInterval _nowT = [nowDate timeIntervalSince1970]*1;
    
    if (_comT - _nowT > 0) {
        return YES;
    }else{
        return NO;
    }
}


+(NSComparisonResult)compareYMDBetweenCriteriondate:(NSDate *)criterionDate andComparedDate:(NSDate *)comparedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString * selectDateString = [dateFormatter stringFromDate:comparedDate];
    //早于当前时间 NO   晚于当前时间 YES
    NSDate *secondDate = [dateFormatter dateFromString:selectDateString];
    
    //转换日期格式
    NSString *firstString = [dateFormatter stringFromDate:criterionDate];;
    
    NSDate *firstDate = [dateFormatter dateFromString:firstString];
    
    NSComparisonResult result = [firstDate compare:secondDate];
    
    return result;
}


+(NSInteger)countMinuteFromTo:(NSString *)compareTime {
    if ([compareTime isNotBlank]) {
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *fromDate = [dateformatter dateFromString:compareTime];
        NSDate *now = [NSDate date];
        
        if ([now timeIntervalSince1970] > [fromDate timeIntervalSince1970]) {
            return 0;
        }
        
        NSTimeInterval time = [fromDate timeIntervalSinceDate:now];
        int minutes = time / 60;
        return minutes;
    }
    else
    {
        return 0;
    }
}


+ (NSComparisonResult)compareDayWithData1:(NSDate *)date1 date2:(NSDate *)date2 {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//设置成中国阳历
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *comps1 = [[NSDateComponents alloc] init];
    comps1 = [calendar components:unitFlags fromDate:date1];
    
    NSDateComponents *comps2 = [[NSDateComponents alloc] init];
    comps2 = [calendar components:unitFlags fromDate:date2];
    
    if ((comps1.year < comps2.year) ||
        (comps1.year == comps2.year && comps1.month < comps2.month) ||
        (comps1.year == comps2.year && comps1.month == comps2.month && comps1.day < comps2.day)) {
        return NSOrderedAscending;
    } else if (comps1.year == comps2.year && comps1.month == comps2.month && comps1.day == comps2.day) {
        return NSOrderedSame;
    } else {
        return NSOrderedDescending;
    }
}

@end
